import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import User from '../views/User.vue'
import Food from '../views/Food.vue'
import Favorite from '../views/Favorite.vue'
import Category from '../views/Category.vue'
import Comment from '../views/Comment.vue'
import Catemenu from '../views/Catemenu.vue'
import store from '../store/index'

Vue.use(VueRouter)



const routes = [
  {
    path: '/',
    redirect: '/admin',
  },
  {
    path: '/admin',
    component: Login
  },
  {
    path: '/admin/home',
    component: Home,
    children: [
      {
        path: '/admin/category',
        name: '/admin/category',
        component: Category
      },
      {
        path: '/admin/food',
        name: '/admin/food',
        component: Food
      },
      {
        path: '/admin/catemenu',
        name: '/admin/catemenu',
        component: Catemenu
      },
      {
        path: '/admin/user',
        name: '/admin/user',
        component: User
      },
      {
        path: '/admin/comment',
        name: '/admin/comment',
        component: Comment
      },
      {
        path: '/admin/favorite',
        name: '/admin/favorite',
        component: Favorite
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// route.beforeEach( (to, from, next) => {
//   if(to.path === '/admin') return next();
//   //const name = window.
// });

router.beforeEach( (to, from, next) =>{
  if(to.path === '/admin') return next();
  const arr = document.cookie.match(new RegExp("(^| )" + 'name.sig' + "=([^;]*)(;|$)"));
  if(!arr) return next('/admin');


  store.commit('setRouteName',to.name);
  next();
} );

export default router
